package com.beagle.njddapp.ui.activity.owner;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpActivity;
import com.beagle.njddapp.bean.owner.NongJiBean;
import com.beagle.njddapp.bean.UploadBean;
import com.beagle.njddapp.bean.ZdBean;
import com.beagle.njddapp.ui.presenter.DeviceAddPresenter;
import com.beagle.njddapp.ui.view.DeviceAddView;
import com.beagle.njddapp.utils.StatusBarUtil;
import com.beagle.njddapp.weight.dialog.ZdDIalog;
import com.blankj.utilcode.util.ToastUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author hxx
 * @date 2021-03-02 22:31
 * 添加nongji
 */
public class DeviceAddActivity extends BaseMvpActivity<DeviceAddView, DeviceAddPresenter> implements DeviceAddView, ZdDIalog.ZdDialogItemClickLisenter {

    @BindView(R.id.commom_title_tv)
    TextView tvTitle;
    @BindView(R.id.common_container)
    RelativeLayout container;
    @BindView(R.id.common_tv_right)
    TextView tvRight;
    @BindView(R.id.ed_device_number)
    EditText edNumber;
    @BindView(R.id.ed_device_creator)
    EditText edChangjia;
    @BindView(R.id.ed_device_addressr)
    EditText eddizhi;
    @BindView(R.id.tv_pinpai)
    TextView tvPinPai;
    @BindView(R.id.tv_type)
    TextView tvLeiXing;
    @BindView(R.id.tv_xinghao)
    TextView tvxinghao;
    @BindView(R.id.ed_device_bianhao)
    EditText edBianhao;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_device_number)
    TextView tvNUmber;
    private String number;
    private boolean isNoNj;//是否无牌农机
    private NongJiBean nongJiBean;
    private String type,pinpai_parentId,xinghao_parentId;
    private ZdDIalog zdDIalog;
    private int nowClick=1;

    @Override
    public int getLayoutId() {
        return R.layout.activity_device_add;
    }


    @Override
    public void init() {
        nongJiBean=new NongJiBean();
        StatusBarUtil.immersive(this, Color.WHITE);
        StatusBarUtil.darkMode(this);
        tvTitle.setText("添加农机设备");
        number=getIntent().getStringExtra("number");
        if (!TextUtils.isEmpty(number)){
            isNoNj=true;
            tvNUmber.setText(number);
            tvNUmber.setVisibility(View.VISIBLE);
            edNumber.setVisibility(View.GONE);
        }


    }

    @Override
    public DeviceAddView getMvpView() {
        return this;
    }

    @Override
    public DeviceAddPresenter createPresenter() {
        return new DeviceAddPresenter();
    }

    @Override
    public void onError(String message) {
        ToastUtils.showShort(message);
    }

    @OnClick({R.id.common_back_iv,R.id.tv_next,R.id.tv_pinpai,R.id.tv_type,R.id.tv_xinghao,R.id.tv_time})
    public void onCLick(View view){
        switch (view.getId()){
            case R.id.common_back_iv:
                onBackPressed();
                break;
            case R.id.tv_pinpai:
                if (TextUtils.isEmpty(tvLeiXing.getText().toString())){
                    ToastUtils.showShort("请先选择类型");
                    return;
                }
                type="2";
                nowClick=2;
                presenter.queryDictList(type,pinpai_parentId);
                break;
            case R.id.tv_type:
                type="1";
                nowClick=1;
                presenter.queryDictList(type,"");
                break;
            case R.id.tv_xinghao:
                if (TextUtils.isEmpty(tvPinPai.getText().toString())){
                    ToastUtils.showShort("请先选择品牌");
                    return;
                }
                type="3";
                nowClick=3;
                presenter.queryDictList(type,xinghao_parentId);
                break;
            case R.id.tv_time:
                chooseTime();
                break;
            case R.id.tv_next:
                if (!isNoNj&&(TextUtils.isEmpty(edNumber.getText().toString())||TextUtils.isEmpty(tvPinPai.getText().toString())
                ||TextUtils.isEmpty(tvLeiXing.getText().toString())||TextUtils.isEmpty(tvxinghao.getText().toString())||
                        TextUtils.isEmpty(edBianhao.getText().toString())||TextUtils.isEmpty(tvTime.getText().toString()))){
                    ToastUtils.showShort("请填写完整信息");
                }else {
                    if (isNoNj){
                        nongJiBean.setMeshNum(tvNUmber.getText().toString());
                    }else {
                        nongJiBean.setMeshNum(edNumber.getText().toString());
                    }
                    nongJiBean.setMfrs(edChangjia.getText().toString());
                    nongJiBean.setMfrsAddress(eddizhi.getText().toString());
                    nongJiBean.setProductionDate(tvTime.getText().toString());
                    nongJiBean.setSerialNo(edBianhao.getText().toString());
                    startActivityForResult(new Intent(this, DeviceAddStepTwoActivity.class).putExtra("data",nongJiBean).putExtra("isNoNj",isNoNj),333);


                }

                break;
        }
    }

    private void chooseTime() {
        Calendar calendar=Calendar.getInstance();
        DatePickerDialog datePickerDialog= new DatePickerDialog( this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                String monthStr;
                if (month<9){
                    monthStr="0"+(month+1);
                }else {
                    monthStr=(month+1)+"";
                }
                tvTime.setText(year+"-" + monthStr + "-" + dayOfMonth);
            }
        }
                ,calendar.get(Calendar.YEAR)
                ,calendar.get(Calendar.MONTH)
                ,calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMaxDate(new Date().getTime());
        datePickerDialog.show();
    }

    @Override
    public void showZd(List<ZdBean> items) {
        if (zdDIalog==null){
            zdDIalog=new ZdDIalog(this);
            zdDIalog.setZdDialogItemClickLisenter(this);
        }
        zdDIalog.show();
        zdDIalog.setZdBeanList(items);
    }

    @Override
    public void uploadSuccess(UploadBean data, int index) {

    }

    @Override
    public void addSuccess() {

    }

    @Override
    public void onZdItemClick(ZdBean zdBean) {
        if (nowClick==1){
            tvLeiXing.setText(zdBean.getName());
            nongJiBean.setType(zdBean.getValue());
            pinpai_parentId=zdBean.getValue();
            tvPinPai.setText("");
            tvxinghao.setText("");
        }else if (nowClick==2){
            tvPinPai.setText(zdBean.getName());
            nongJiBean.setBrand(zdBean.getValue());
            xinghao_parentId=zdBean.getValue();
            tvxinghao.setText("");
        }else {
            tvxinghao.setText(zdBean.getName());
            nongJiBean.setSize(zdBean.getValue());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==310){
            setResult(311);
            finish();
        }
    }
}
